package site.shenjie.eureka.ribbon.feign.config;

import feign.Client;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 沈杰
 * 2020-7-27 10:43:01
 * 配置类：OkHttp配置
 * 其通过 @ConditionalOnProperty 中的值来决定启用哪种客户端（HttpClient 和 OkHttp），
 * 其中@ConditionalOnClass 表示对应的类在 classpath 目录下存在时，才会去解析对应的配置文件。
 */
@Configuration
@ConditionalOnClass(OkHttpClient.class)
@ConditionalOnMissingClass("com.netflix.loadbalancer.ILoadBalancer")
@ConditionalOnProperty(value = "feign.okhttp.enabled", matchIfMissing = true)
public class OkHttpFeignConfiguration {
    @Autowired
    private OkHttpClient okHttpClient;

    @Bean
    @ConditionalOnMissingBean(Client.class)
    public Client feignClient() {
        if (this.okHttpClient != null)
            return new feign.okhttp.OkHttpClient(this.okHttpClient);
        return new feign.okhttp.OkHttpClient();
    }
}
